package com.yiboshi.rehrs.domain.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yiboshi.rehrs.common.VarConstant;
import com.yiboshi.rehrs.domain.entity.InviteRecord;
import com.yiboshi.rehrs.domain.entity.SmZhongUser;
import com.yiboshi.rehrs.domain.entity.ZhongUser;
import com.yiboshi.rehrs.domain.mapper.InviteRecordMapper;
import com.yiboshi.rehrs.domain.service.IInviteRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiboshi.rehrs.domain.service.ISystemConfigService;
import com.yiboshi.rehrs.domain.service.IZhongUserService;
import com.yiboshi.rehrs.param.ZhongRes;
import com.yiboshi.rehrs.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author local
 * @since 2025-06-12
 */
@Service
public class InviteRecordServiceImpl extends AperBaseServiceImpl<InviteRecordMapper, InviteRecord> implements IInviteRecordService {

    @Autowired
    IZhongUserService zhongUserService;
    @Autowired
    ISystemConfigService systemConfigService;
    public LocalDateTime updateInviteRecord(String card,ZhongUser sessionZhongUser,  ZhongUser inviteUser){
        InviteRecord inviteRecord = new InviteRecord();
        inviteRecord.setInviteCode(card);
        inviteRecord.setOriginAccount(inviteUser.getAccount());
        inviteRecord.setOriginStatus(0);
        inviteRecord.setUsedAccount(sessionZhongUser.getAccount());
        inviteRecord.setId(IdWorker.getId());
        int days= 5;//systemConfigService.exchangeVipDays();
        //动态设置天数 一个人赠送的天数不能太多
        LambdaQueryWrapper<InviteRecord> queryWrapper = this.getQueryWrapper().eq(InviteRecord::getOriginAccount, inviteUser.getAccount());
        int count = this.count(queryWrapper);
        int vipDays = 1;
        if(count==0){
            vipDays = 5;
        }else if(count==1){
            vipDays = 2;
        }
        inviteRecord.setVipDays(vipDays);
        this.save(inviteRecord);
        Long id = sessionZhongUser.getId();
        ZhongUser zhongUser = zhongUserService.getById(id);
        LocalDateTime vipExpDate = zhongUser.getVipExpDate();
        LocalDateTime localDateTime = DateUtils.plusDays(vipExpDate, 2);//使用邀请码固定送2天
        zhongUserService.update(zhongUserService.getUpdateWrapper().eq(ZhongUser::getId, id).set(ZhongUser::getVipExpDate,localDateTime));
        return localDateTime;
        //    return ZhongRes.ok("操作成功，VIP+"+days+"，到期时间为："+DateUtils.dateTimeToStr(localDateTime, DateUtils.CH_TIME));

    }

    @Override
    public List<InviteRecord> listOriginAccount(String account) {
        return baseMapper.listOriginAccount(account);
    }

    @Override
    public int exchange(SmZhongUser smUserInfo, Integer unrealizedNum) {
        LambdaQueryWrapper<InviteRecord> queryWrapper = getQueryWrapper().eq(InviteRecord::getOriginAccount, smUserInfo.getAccount())
                .eq(InviteRecord::getOriginStatus, 0);
        List<InviteRecord> inviteRecords = this.list(queryWrapper);
        if (CollUtil.isEmpty(inviteRecords)) {
            return 0;
        }
        List<Long> ids = new ArrayList<>();
        int vipDays = 0;
        for (InviteRecord inviteRecord : inviteRecords) {
            ids.add(inviteRecord.getId());
            vipDays = vipDays + inviteRecord.getVipDays();
        }
        LambdaUpdateWrapper<InviteRecord> updateWrapper = getUpdateWrapper()
                .in(InviteRecord::getId, ids)
                .set(InviteRecord::getUpdateTime,LocalDateTime.now())
                .set(InviteRecord::getOriginStatus, 1);
        int unrealizedNumDB = this.getBaseMapper().update(null, updateWrapper);
        if (unrealizedNumDB < 0) {
            return 0;
        }
        ZhongUser one = zhongUserService.getOne(smUserInfo.getAccount(), 1);
        LocalDateTime vipExpDate = one.getVipExpDate();
        vipExpDate = vipExpDate==null||vipExpDate.isBefore(LocalDateTime.now())?LocalDateTime.now():vipExpDate;
        LocalDateTime localDateTime = DateUtils.plusDays(vipExpDate, vipDays);
        LambdaUpdateWrapper<ZhongUser> zhongUserLambdaUpdateWrapper = zhongUserService.getUpdateWrapper()
                .eq(ZhongUser::getId, smUserInfo.getId())
                .set(ZhongUser::getVipExpDate, localDateTime);
        zhongUserService.update(zhongUserLambdaUpdateWrapper);
        return vipDays;
    }
}
